﻿using System.Windows.Media;
using Brjnk.Common;

namespace Brjnk.GpxExplorer.Workspace.BitmapExport
{
    public class LabelStyle : InheritableValues
    {
        static LabelStyle()
        {
            LabelStyle.DefineInheritableValue("Background", Colors.White);
            LabelStyle.DefineInheritableValue("BackgroundOpacity", .8);
            LabelStyle.DefineInheritableValue("Border", Colors.Black);
            LabelStyle.DefineInheritableValue("BorderOpacity", 0.8);
            LabelStyle.DefineInheritableValue("BorderThickness", 1.0);
            LabelStyle.DefineInheritableValue("ContentRotation", 0.0);
            LabelStyle.DefineInheritableValue("FontSize", 13.0);
            LabelStyle.DefineInheritableValue("FontColor", Colors.Black);
        }

        public LabelStyle() { }

        public LabelStyle(LabelStyle parent)
            : base(parent)
        { CreateBackgroundBrush(); CreateBorderBrush(); CreateBackgroundBrush(); }

        private void CreateBackgroundBrush()
        {
            var color = Background;
            color.A = (byte)(BackgroundOpacity * 255);
            BackgroundBrush = new SolidColorBrush(color);
        }

        private void CreateBorderBrush()
        {
            var color = Border;
            color.A = (byte)(BorderOpacity * 255);
            BorderBrush = new SolidColorBrush(color);
        }

        private Brush backgroundBrush = Brushes.White;

        private Brush borderBrush = Brushes.Black;

        //public properties

        public Color Background
        {
            get { return base.GetValue<Color>("Background"); }
            set { base.SetValue("Background", value); }
        }

        public Brush BackgroundBrush
        {
            get { return backgroundBrush; }
            private set { backgroundBrush = value; OnPropertyChanged("BackgroundBrush"); }
        }

        public double BackgroundOpacity
        {
            get { return base.GetValue<double>("BackgroundOpacity"); }
            set { base.SetValue("BackgroundOpacity", value); }
        }

        public Color Border
        {
            get { return base.GetValue<Color>("Border"); }
            set { base.SetValue("Border", value); }
        }

        public Brush BorderBrush
        {
            get { return borderBrush; }
            private set { borderBrush = value; OnPropertyChanged("BorderBrush"); }
        }

        public double BorderOpacity
        {
            get { return base.GetValue<double>("BorderOpacity"); }
            set { base.SetValue("BorderOpacity", value); }
        }

        public double BorderThickness
        {
            get { return base.GetValue<double>("BorderThickness"); }
            set { base.SetValue("BorderThickness", value); }
        }

        public double ContentRotation
        {
            get { return base.GetValue<double>("ContentRotation"); }
            set { base.SetValue("ContentRotation", value); }
        }

        public double FontSize
        {
            get { return base.GetValue<double>("FontSize"); }
            set { base.SetValue("FontSize", value); }
        }

        public Color FontColor
        {
            get { return base.GetValue<Color>("FontColor"); }
            set { base.SetValue("FontColor", value); }
        }

        //IsLocalProperties
        public bool IsBackgroundLocal
        {
            get { return IsLocalGetter("Background"); }
            set { IsLocalSetter("Background", value); }
        }

        public bool IsBackgroundOpacityLocal
        {
            get { return IsLocalGetter("BackgroundOpacity"); }
            set { IsLocalSetter("BackgroundOpacity", value); }
        }

        public bool IsBorderLocal
        {
            get { return IsLocalGetter("Border"); }
            set { IsLocalSetter("Border", value); }
        }

        public bool IsBorderOpacityLocal
        {
            get { return IsLocalGetter("BorderOpacity"); }
            set { IsLocalSetter("BorderOpacity", value); }
        }

        public bool IsBorderThicknessLocal
        {
            get { return IsLocalGetter("BorderThickness"); }
            set { IsLocalSetter("BorderThickness", value); }
        }

        public bool IsContentRotationLocal
        {
            get { return IsLocalGetter("ContentRotation"); }
            set { IsLocalSetter("ContentRotation", value); }
        }

        public bool IsFontSizeLocal
        {
            get { return base.IsLocalGetter("FontSize"); }
            set { base.IsLocalSetter("FontSize", value); }
        }

        public bool IsFontColorLocal
        {
            get { return base.IsLocalGetter("FontColor"); }
            set { base.IsLocalSetter("FontColor", value); }
        }

        protected override void ProcessPropertyChange(string propertyName)
        {
            if (propertyName == "Background" || propertyName == "BackgroundOpacity")
                CreateBackgroundBrush();
            if (propertyName == "Border" || propertyName == "BorderOpacity")
                CreateBorderBrush();
        }
    }
}